//
//  UIBarButtonItem+Extensions.swift
//  MyBlog
//
//  Created by victor on 16/12/13.
//  Copyright © 2016年 victor. All rights reserved.
//

extension UIBarButtonItem {
    /*
     convenience 遍历构造函数,最主要的作用简化控件创建
     */
    
    /// 创建UIBarButtonItem
    ///
    /// - Parameters:
    ///   - title: title
    ///   - fontSize: fontSize 默认16
    ///   - target: target
    ///   - action: action
    ///   - isBack: 是否是返回按钮，如果是加上箭头
    convenience init(title:String,fontSize:CGFloat = 16 ,target:AnyObject? ,action:Selector, isBack:Bool = false) {
        
        // cz_textButton 是oc的方法，返回值是instancetype 是可选的，所以为了避免出现解包，要制定button的 类型
        let button : UIButton = UIButton.cz_textButton(title, fontSize: fontSize, normalColor: UIColor.darkGray, highlightedColor: UIColor.orange)
        
        if isBack {
            let imageName = "navigationbar_back_withtext"
            
            button.setImage(UIImage(named:imageName), for: .normal)
            button.setImage(UIImage(named:imageName + "_highlighted"), for: .highlighted)
            
            //如果不加这句，就会显示不全
            button.sizeToFit()
        }
        
        
        button.addTarget(target, action: action, for: .touchUpInside)
        
        //self.init 实例化 UIBarButtonItem
        self.init(customView:button)
    }
}
